import pandas as pd
import numpy as np

def feature_engineering(data, event_name):
    """
    Perform feature engineering on the given dataset.

    Parameters:
        data (pd.DataFrame): Original dataset with financial indicators.
        event_name (str): Name of the event for creating event-specific features.

    Returns:
        pd.DataFrame: Dataset with engineered features.
    """
    # Ensure 'Timestamp' column is in datetime format
    data['Timestamp'] = pd.to_datetime(data['Timestamp'])

    # Create Shock Indicator (1 for during event, 0 otherwise)
    data['Shock_Indicator'] = 1  # Assuming all rows are during the event; modify if needed

    # Lagged Variables (example: Stock_Index at t-1, t-2)
    data['Stock_Index_Lag_1'] = data['Stock_Index'].shift(1)
    data['Stock_Index_Lag_2'] = data['Stock_Index'].shift(2)

    # Rolling Volatility (example: 7-period rolling standard deviation for Stock_Index)
    data['Stock_Index_Volatility'] = data['Stock_Index'].rolling(window=7).std()

    # Moving Average (example: 7-period moving average for Stock_Index)
    data['Stock_Index_MA_7'] = data['Stock_Index'].rolling(window=7).mean()

    # Rolling Correlation (example: correlation between Stock_Index and Bond_Yield_10Y_Treasury)
    if 'Bond_Yield_10Y_Treasury' in data.columns:
        data['Stock_Bond_Correlation'] = data['Stock_Index'].rolling(window=7).corr(data['Bond_Yield_10Y_Treasury'])

    # Time-Based Features
    data['Hour'] = data['Timestamp'].dt.hour
    data['Day'] = data['Timestamp'].dt.day
    data['Month'] = data['Timestamp'].dt.month

    # Interaction Terms (e.g., Shock Indicator with Stock_Index)
    data['Shock_Stock_Interaction'] = data['Shock_Indicator'] * data['Stock_Index']

    # Normalize Trading Volume (if Trading Volume column exists)
    if 'Trading_Volume' in data.columns:
        data['Normalized_Trading_Volume'] = (data['Trading_Volume'] - data['Trading_Volume'].mean()) / data['Trading_Volume'].std()

    # Fill NaN values generated by rolling or lagging
    data.fillna(method='bfill', inplace=True)  # Backfill for continuity
    data.fillna(method='ffill', inplace=True)  # Forward fill as a backup

    return data

# File paths and event names
datasets = {
    "Global_Financial_Crisis": "C:\\Users\\HP\\Desktop\\Global_Financial_Crisis_financial_data.csv",
    "Brexit": "C:\\Users\\HP\\Desktop\\brexit_financial_data.csv",
    "COVID_19_Crisis": "C:\\Users\\HP\\Desktop\\covid_19_crisis_financial_data.csv",
    "Eurozone_Debt_Crisis": "C:\\Users\\HP\\Desktop\\eurozone_debt_crisis_financial_data.csv",
    "Quantitative_Easing": "C:\\Users\\HP\\Desktop\\quantitative_easing_financial_data.csv",
    "Dodd_Frank_Act": "C:\\Users\\HP\\Desktop\\dodd_frank_act_financial_data.csv"
}

# Process each dataset and save the engineered features
for event_name, file_path in datasets.items():
    try:
        # Load the dataset
        data = pd.read_csv(file_path)
        
        # Perform feature engineering
        engineered_data = feature_engineering(data, event_name)
        
        # Save the processed file
        output_path = f"C:\\Users\\HP\\Desktop\\processed_{event_name.lower().replace(' ', '_')}_features.csv"
        engineered_data.to_csv(output_path, index=False)
        print(f"Feature engineering completed and saved for {event_name}: {output_path}")
    except FileNotFoundError:
        print(f"File not found: {file_path}. Please provide the correct path.")
    except Exception as e:
        print(f"An error occurred while processing {event_name}: {e}")
